package com.example.kugou.common.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeAdd {
   static Calendar cal = Calendar.getInstance();
   static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static Calendar cal1 = Calendar.getInstance();
    /**
     * 添加时间，添加的单位为天
     * @param date
     * @param month
     */
    public static void timeAddMonth(Date date,int month){
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
    }

    public static void timeAddDay(Date date,int day){
        cal.setTime(date);
        cal.add(Calendar.DATE, day);
    }

    /**
     * 当前时间添加月份
     * @param month
     */
    public static void nowAddMonth(int month){
        cal.add(Calendar.MONTH, month);
    }

    /**
     * 获取显示的时间年月日时分秒
     */
    public static String getTime(){
        //获取设置时间的年月日时分秒
        int year = cal.get(Calendar.YEAR);
        //获取到的月份比实际的少一个月，应+1
        int month = cal.get(Calendar.MONTH)+1;
        int day= cal.get(Calendar.DATE);
        int hour = cal.get(Calendar.HOUR);
        int min = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        cal1.set(year,month,day);
        String time = year+"-"+month+"-"+day+" "+hour+":"+min+":"+second;
        System.out.println("该时间的年月日时分秒："+time);
        return time;
    }

    /**
     * 获取显示的时间年月日时分秒
     */
    public static String getTime02(){
        //获取设置时间的年月日时分秒
        int year = cal.get(Calendar.YEAR);
        //获取到的月份比实际的少一个月，应+1
        int month = cal.get(Calendar.MONTH)+1;
        int day= cal.get(Calendar.DATE);
        cal1.set(year,month,day);
        String time = year+"-"+month+"-"+day;
        System.out.println("该时间的年月日时分秒："+time);
        return time;
    }

    /**
     * 获取传入时间的转换为字符串的格式
     * @return
     * @throws ParseException
     */
    public static String getDateNow(Date date) throws ParseException {
        Calendar now = Calendar.getInstance();
        System.out.println("年: " + now.get(Calendar.YEAR));
        System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
        System.out.println("分: " + now.get(Calendar.MINUTE));
        System.out.println("秒: " + now.get(Calendar.SECOND));
        System.out.println("当前时间毫秒数：" + now.getTimeInMillis());
        System.out.println(now.getTime());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(date);
        System.out.println("格式化后的日期：" + dateNowStr);
        return dateNowStr;
    }


    /**
     * 获取包含星期的日期显示
     * @return
     */
    public static String getAllAndWeek(){
        //对于创建SimpleDateFormat传入的参数：EEEE代表星期，如“星期四”；MMMM代表中文月份，如“七月”；MM代表月份，如“07”；yyyy代表年份，
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEEE");
        //获取设置时间的星期几
        int week = cal1.get(Calendar.DAY_OF_WEEK) - 1;
        return format2.format(cal1.getTime());
    }

    /**
     * 获取日期格式的形式
     * @return
     */
    public static Date getDate(){
        return cal.getTime();
    }


    /**
     * 大于0    第一个大于第二个
     * 等于0    两个日期相等
     * 小于0    第一个小于第二个
     * @param str1
     * @param str2
     * @return
     */
    public static int checkStringDate(String str1,String str2){
        int res=str1.compareTo(str2);
        if(res>0)
            System.out.println("str1>str2");
        else if(res==0)
            System.out.println("str1=str2");
        else
            System.out.println("str1<str2");
        return res;
    }

    /**
     * true    第一个时间在第二个时间之后，包括等于
     * false   第一个时间在第二个时间之前
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean after(Date date1,Date date2){
        boolean b = date1.after(date2);
        if (b){
            //如果date1的时间在date2之后（不包括等于）返回true
            System.out.println("第一个时间在第二个时间之后，包括等于");
        }else {
            System.out.println("第一个时间在第二个时间之前");
        }
        return b;
    }

    public static String getTimeString(int time) {
        int miao = time % 60;
        int fen = time / 60;
        int hour = 0;
        if (fen >= 60) {
            hour = fen / 60;
            fen = fen % 60;
        }
        String timeString = "";
        String miaoString = "";
        String fenString = "";
        String hourString = "";
        if (miao < 10) {
            miaoString = "0" + miao;
        } else {
            miaoString = miao + "";
        }
        if (fen < 10) {
            fenString = "0" + fen;
        } else {
            fenString = fen + "";
        }
        if (hour < 10) {
            hourString = "0" + hour;
        } else {
            hourString = hour + "";
        }
        if (hour != 0) {
            timeString = hourString + ":" + fenString + ":" + miaoString;
        } else {
            timeString = fenString + ":" + miaoString;
        }
        return timeString;
    }

}
